//
//  AddImgCell.m
//  efairy
//
//  Created by 李锋 on 2019/3/4.
//  Copyright © 2019年 East. All rights reserved.
//

#import "AddImgCell.h"
#import "EFANoticePubCCell.h"
#import "EFAModel.h"

@interface AddImgCell()<UICollectionViewDelegate, UICollectionViewDataSource, EFANoticePubCCellDelegate>
@property (nonatomic, readonly) NSInteger imgMaxCnt;
@property (nonatomic, readonly) NSArray<EFANotPubItem *> *image_list;


@end
static NSString *const kReuseCellId = @"EFANoticePubCCell";
@implementation AddImgCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self initView];
    }
    return self;
}
-(void)drawRect:(CGRect)rect{
    //左上角 右上角圆角
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: _titleUIView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(6,6)];
    //创建 layer
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = _titleUIView.bounds;
    //赋值
    maskLayer.path = maskPath.CGPath;
    _titleUIView.layer.mask = maskLayer;
    
    //左下角 右下角圆角
    UIBezierPath *maskPath2 = [UIBezierPath bezierPathWithRoundedRect: _collectionView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(6,6)];
    //创建 layer
    CAShapeLayer *maskLayer2 = [[CAShapeLayer alloc] init];
    maskLayer2.frame = _collectionView.bounds;
    //赋值
    maskLayer2.path = maskPath2.CGPath;
    _collectionView.layer.mask = maskLayer2;
}

-(void)initView{
    _titleUIView = [[UIView alloc]init];
    _titleUIView.backgroundColor = [UIColor whiteColor];
    
    _titleLabel = [[UILabel alloc]init];
    _titleLabel.text = @"处理图片";
    _titleLabel.font = [UIFont systemFontOfSize:15.f];
    _titleLabel.textColor = ColorFromRGB(0x333333);
    _titleLabel.backgroundColor = [UIColor whiteColor];
    
   
    [_titleUIView addSubview:_titleLabel];
    _titleLabel.sd_layout.heightIs(15).leftSpaceToView(_titleUIView, 10)
    .rightSpaceToView(_titleUIView, 10).topSpaceToView(_titleUIView, 14.5);
    
//    _collectionView = [[UICollectionView alloc]init];
//   _collectionView.delegate = self;
//    _collectionView.dataSource = self;
    
    _layout = [[UICollectionViewFlowLayout alloc] init];
//    _layout.itemSize = CGSizeMake(305, 305);
    /**
     创建collectionView
     */
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
//    self.collectionView.alwaysBounceVertical = YES;//collectionView 内容可滑动
    
    /**
     注册item和区头视图、区尾视图
     */
    UINib *cellNib = [UINib nibWithNibName:kReuseCellId bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:kReuseCellId];
    
//    [_imgView sd_addSubviews:@[_titleLabel,_collectionView]];
    
//    _titleLabel.sd_layout.heightIs(44).leftSpaceToView(_imgView, 8)
//    .rightSpaceToView(_imgView, 8).topSpaceToView(_imgView, 0);
//
//    self.collectionView.sd_layout.leftSpaceToView(_imgView, 8).rightSpaceToView(_imgView, 8)
//        .topSpaceToView(_titleLabel, 1);
    
    _submitBtn = [[UIButton alloc]init];
    [_submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    [_submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _submitBtn.titleLabel.font = [UIFont systemFontOfSize:17.f];
    [_submitBtn setBackgroundColor:ColorFromRGB(0x282449)];
    _submitBtn.layer.cornerRadius = 24;
    [_submitBtn addTarget:self action:@selector(onClickSubmitBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView sd_addSubviews:@[_titleUIView,_collectionView,_submitBtn]];
    
//    _imgView.sd_layout.leftSpaceToView(self.contentView, 0).rightSpaceToView(self.contentView, 0)
//    .topSpaceToView(_titleLabel, 1);
        _titleUIView.sd_layout.heightIs(44).leftSpaceToView(self.contentView, 8)
        .rightSpaceToView(self.contentView, 8)
        .topSpaceToView(self.contentView, 0);
    
        self.collectionView.sd_layout.leftSpaceToView(self.contentView, 8)
            .rightSpaceToView(self.contentView, 8)
            .topSpaceToView(_titleUIView, 1);
    _submitBtn.sd_layout.heightIs(48).leftSpaceToView(self.contentView, 25)
    .rightSpaceToView(self.contentView, 25)
    .topSpaceToView(self.collectionView, 21);
    
    self.contentView.backgroundColor = ColorFromRGB(0xeff0f4);
    [self setupAutoHeightWithBottomView:_submitBtn bottomMargin:12];
    
    
    
}




#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.devModel) {
        return (self.devModel.image_list.count < self.devModel.imgMaxCnt) ? self.devModel.image_list.count + 1 : self.devModel.imgMaxCnt;
    }
    return (self.image_list.count < self.imgMaxCnt) ? self.image_list.count + 1 : self.imgMaxCnt;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    EFANoticePubCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kReuseCellId forIndexPath:indexPath];
    if (self.devModel) {
        EFADevPubItem *item = nil;
        if (indexPath.row < self.devModel.image_list.count) {
            item = self.devModel.image_list[indexPath.row];
        }
        [cell configViewWithDevPubItem:item];
    } else {
        EFANotPubItem *item = nil;
        if (indexPath.row < self.image_list.count) {
            item = self.image_list[indexPath.row];
        }
        [cell configViewWithNotPubItem:item];
    }
    cell.delegate = self;
    return cell;
}



//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.section == 0 && [kind isEqualToString:UICollectionElementKindSectionHeader]) {
//        EFANoticeEditCRView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kReuseCRViewId forIndexPath:indexPath];
//        header.pubModel = self.pubModel;
//        return header;
//    }
//    return nil;
//}

#pragma mark - <UICollectionViewDelegateFlowLayout>
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//    return CGSizeMake(CGRectGetWidth(self.collectionView.bounds), 254.0);
//}

//每个Cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = (CGRectGetWidth([UIScreen mainScreen].bounds) - 2*(10+8) - 3 * 10) / 4.0;
    return CGSizeMake(width, width);
}

//四周边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

////行间距
//-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
//    return 10;
//}
//
////列间距
//-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
//    return 10;
//
//}


#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.devModel) {
        if (indexPath.row == self.devModel.image_list.count) {
            if ([self.delegate respondsToSelector:@selector(matAddImgTCell:collectionView:addWithIndexPath:)]) {
                [self.delegate matAddImgTCell:self collectionView:collectionView addWithIndexPath:indexPath];
            }
        }
    } else {
        if (indexPath.row == self.image_list.count) {
            if ([self.delegate respondsToSelector:@selector(matAddImgTCell:collectionView:addWithIndexPath:)]) {
                [self.delegate matAddImgTCell:self collectionView:collectionView addWithIndexPath:indexPath];
            }
        }
    }
}

#pragma mark - EFANoticePubCCellDelegate
- (void)noticePubCCell:(EFANoticePubCCell *)cell closeBtnTapped:(UIButton *)btn {
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
    if ([self.delegate respondsToSelector:@selector(matAddImgTCell:collectionView:delWithIndexPath:)]) {
        [self.delegate matAddImgTCell:self collectionView:self.collectionView delWithIndexPath:indexPath];
    }
}



- (void)configViewWithMtcPubModel:(EFAMtcPubModel *)model {
    self.model = model;
    
    NSInteger cnt = (self.image_list.count < self.imgMaxCnt) ? self.image_list.count + 1 : self.imgMaxCnt;
    NSInteger row = cnt / 4 + ((cnt % 4) ? 1 : 0) ;
    CGFloat width = (CGRectGetWidth([UIScreen mainScreen].bounds) - 2*(10+8) - 3 * 10) / 4.0;
    CGFloat cViewHeight = 10 * 2 + width * row + row * 10+ 34.f;
//    self.collectionViewHeight.constant = cViewHeight;
      _collectionView.height = cViewHeight;
    
    
    [self.collectionView reloadData];
}

#pragma mark - getters and setters
- (NSInteger)imgMaxCnt {
    if (self.model) {
        return self.model.imgMaxCnt;
    } else if (self.alarmModel) {
        return self.alarmModel.imgMaxCnt;
    } else {
        return self.dangerModel.imgMaxCnt;
    }
}
- (NSArray<EFANotPubItem *> *)image_list {
    if (self.model) {
        return self.model.image_list;
    } else if (self.alarmModel) {
        return self.alarmModel.image_list;
    } else {
        return self.dangerModel.image_list;
    }
}

//点击提交按钮
-(void)onClickSubmitBtn{
    if(self.submitBtnBlock){
        self.submitBtnBlock();
    }
}

@end


@implementation AddImgCell (AlarmRelative)
- (void)configViewWithAlarmPubModel:(EFAAlarmPubModel *)model {
    self.alarmModel = model;
    
    NSInteger cnt = (self.alarmModel.image_list.count < self.alarmModel.imgMaxCnt) ? self.alarmModel.image_list.count + 1 : self.alarmModel.imgMaxCnt;
    NSInteger row = cnt / 4 + ((cnt % 4) ? 1 : 0) ;
    CGFloat width = (CGRectGetWidth([UIScreen mainScreen].bounds) - 2*(10+8) - 3 * 10) / 4.0;
    CGFloat cViewHeight = 10 * 2 + width * row + row  * 10+34.f;
//    self.collectionViewHeight.constant = cViewHeight;
    _collectionView.height = cViewHeight;
    
    
    [self.collectionView reloadData];
}
@end


@implementation AddImgCell (DangerRelative)
- (void)configViewWithDangerPubModel:(EFADangerPubModel *)model {
    self.dangerModel = model;
    
    NSInteger cnt = (model.image_list.count < model.imgMaxCnt) ? model.image_list.count + 1 : model.imgMaxCnt;
    NSInteger row = cnt / 4 + ((cnt % 4) ? 1 : 0) ;
    CGFloat width = (CGRectGetWidth([UIScreen mainScreen].bounds) - 2*(10+8) - 3 * 10) / 4.0;
    CGFloat cViewHeight = 10 * 2 + width * row + row * 10+ 34.f;
//    self.collectionViewHeight.constant = cViewHeight;
      _collectionView.height = cViewHeight;
    
    
    
    [self.collectionView reloadData];
}
@end


@implementation AddImgCell (DeviceRelative)
- (void)configViewWithDevPubModel:(EFADevPubModel *)model {
    self.devModel = model;
    
    NSInteger cnt = (model.image_list.count < model.imgMaxCnt) ? model.image_list.count + 1 : model.imgMaxCnt;
    NSInteger row = cnt / 4 + ((cnt % 4) ? 1 : 0) ;
    CGFloat width = (CGRectGetWidth([UIScreen mainScreen].bounds) - 2*(10+8) - 3 * 10) / 4.0;
    CGFloat cViewHeight = 10 * 2 + width * row + row * 10+ 34.f;
//    self.collectionViewHeight.constant = cViewHeight;
      _collectionView.height = cViewHeight;
    
    
    [self.collectionView reloadData];
}
@end

